package com.fcscs.cservice.ws.endpoint;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ws.server.endpoint.annotation.Endpoint;
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
import org.springframework.ws.server.endpoint.annotation.RequestPayload;
import org.springframework.ws.server.endpoint.annotation.ResponsePayload;

import com.fcscs.core.common.MainEnums;
import com.fcscs.core.util.CommonUtil;
import com.fcscs.cservice.domain.model.GuestProfileFindView;
import com.fcscs.cservice.domain.model.GuestProfileView;
import com.fcscs.cservice.service.GuestProfileManager;
import com.fcscs.cservice.ws.schema.guestprofileservice.FindGuestProfileRequest;
import com.fcscs.cservice.ws.schema.guestprofileservice.FindGuestProfileResponse;
import com.fcscs.cservice.ws.schema.guestprofileservice.GetGuestProfileRequest;
import com.fcscs.cservice.ws.schema.guestprofileservice.GetGuestProfileResponse;
import com.fcscs.cservice.ws.schema.guestprofileservice.UpdateGuestProfileRequest;
import com.fcscs.cservice.ws.schema.guestprofileservice.UpdateGuestProfileResponse;
import com.fcscs.share.util.TimezoneUtils;

@Endpoint
public class GuestProfileServiceEndpoint {

	@Autowired
	GuestProfileManager gpMgr;
	
	@PayloadRoot(localPart = CServiceWSConstants.FIND_GP_REQ, namespace = CServiceWSConstants.GP_SERVICE_NAMESPACE_URI)
	@ResponsePayload
	public FindGuestProfileResponse findGP(@RequestPayload FindGuestProfileRequest req) {
		FindGuestProfileResponse resp = new FindGuestProfileResponse();

		List<GuestProfileFindView> gpList = gpMgr.findGuestProfile(CommonUtil.getWsSessionPropertyId(), req.getFirstName(), req.getLastName(),
				req.getRoomNo(), req.getPhoneNo(), req.getMobileNo(), req.getMembershipNo(), req.getStart(), req.getNumResults(),
				req.getInHouseGuestOnly() == null ? false : req.getInHouseGuestOnly());
		int count = gpMgr.findGuestProfileCnt(CommonUtil.getWsSessionPropertyId(), req.getFirstName(), req.getLastName(),
				req.getRoomNo(), req.getPhoneNo(), req.getMobileNo(), req.getMembershipNo(), req.getInHouseGuestOnly() == null ? false : req.getInHouseGuestOnly());
		MainEnums.TimeZone tz = TimezoneUtils.getTimezone(CommonUtil.getWsSessionPropertyId());
		convGPViewToLocalTime(gpList, tz);
		resp.setGuestProfileViewList(gpList);
		resp.setTotalCnt(count);
		return resp;
	}

	@PayloadRoot(localPart = CServiceWSConstants.GET_GP_REQ, namespace = CServiceWSConstants.GP_SERVICE_NAMESPACE_URI)
	@ResponsePayload
	public GetGuestProfileResponse getGP(@RequestPayload GetGuestProfileRequest req) {
		GetGuestProfileResponse resp = new GetGuestProfileResponse();

		GuestProfileView gpv = gpMgr.getGuestProfileView(req.getGuestProfileId());
		MainEnums.TimeZone tz = TimezoneUtils.getTimezone(CommonUtil.getWsSessionPropertyId());
		convGPViewToLocalTime(gpv, tz);
		resp.setGuestProfileView(gpv);
		return resp;
	}

	@PayloadRoot(localPart = CServiceWSConstants.UPDATE_GP_REQ, namespace = CServiceWSConstants.GP_SERVICE_NAMESPACE_URI)
	@ResponsePayload
	public UpdateGuestProfileResponse updateGP(@RequestPayload UpdateGuestProfileRequest req) {
		UpdateGuestProfileResponse resp = new UpdateGuestProfileResponse();
		GuestProfileView gpv = req.getGuestProfileView();
		gpv.setUpdated(new Date());
		gpv.setOrgId(CommonUtil.getWsSessionOrgId());
		gpv.setPropertyId(CommonUtil.getWsSessionPropertyId());
		gpv = gpMgr.updateGuestProfileView(gpv);
		resp.setGuestProfileView(gpv);
		resp.setSuccess(true);
		return resp;
	}

	private void convGPViewToLocalTime(List<GuestProfileFindView> gpList, MainEnums.TimeZone tz) {
		if (gpList == null) {
			return;
		}
		for (GuestProfileFindView gpv : gpList) {
			convGPViewToLocalTime(gpv, tz);
		}
	}

	private void convGPViewToLocalTime(GuestProfileFindView gpv, MainEnums.TimeZone tz) {
		if (gpv == null) {
			return;
		}
		gpv.setCreated(TimezoneUtils.convertToLocalTimeZone(gpv.getCreated(), tz));
		gpv.setUpdated(TimezoneUtils.convertToLocalTimeZone(gpv.getUpdated(), tz));
		gpv.setArrivalDateTime(TimezoneUtils.convertToLocalTimeZone(gpv.getArrivalDateTime(), tz));
		gpv.setDepartureDateTime(TimezoneUtils.convertToLocalTimeZone(gpv.getDepartureDateTime(), tz));
	}

	private void convGPViewToLocalTime(GuestProfileView gpv, MainEnums.TimeZone tz) {
		if (gpv == null) {
			return;
		}
		gpv.setCreated(TimezoneUtils.convertToLocalTimeZone(gpv.getCreated(), tz));
		gpv.setUpdated(TimezoneUtils.convertToLocalTimeZone(gpv.getUpdated(), tz));
	}
}
